package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import org.apache.catalina.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 郝文雪
 * @date 2022/8/3 18:00
 * @Description Test
 */
@RequestMapping("/user")
@RestController
public class UseController {

    /**
     * 注册功能
     * @param userInfo
     * @return
     */
    @RequestMapping("/reg")
    public boolean reg(UserInfo userInfo){
        //伪代码
//        int num=10/0;
//        Object obj=null;
//        obj.hashCode();
//        int num=Integer.valueOf("abc");
        return true;
    }

    /**
     * 上传头像
     * @return
     */
    @RequestMapping("/upload")
    public UserInfo upload(){
        UserInfo userInfo=new UserInfo();
        userInfo.setId(1);
        userInfo.setUsername("张三");
        userInfo.setPassword("");
        return userInfo;
    }

    @RequestMapping("/login")
    public boolean login(String username, String password, HttpServletRequest request){
        //1.非空校验
        if(StringUtils.hasLength(username) && StringUtils.hasLength(password)){
            if(username.equals("admin") && password.equals("admin")){
                //用户名和密码成功
                //添加session
                HttpSession session=request.getSession(true);
                session.setAttribute("userinfo",new UserInfo(1,"admin","admin"));
                return true;
            }
        }
        return false;
    }
}
